//package com.preparation.controller;
//
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.preparation.domain.Post;
//import com.preparation.domain.PostComment;
//import com.preparation.dto.R;
//import com.preparation.service.IPostCommentService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.annotation.*;
//
//import javax.validation.Valid;
//
//@CrossOrigin
//@RestController
//@RequestMapping("/comments")
//public class PostCommentController {
//
//    @Autowired
//    private IPostCommentService commentService;
//
//
//    //分页查询
//    @GetMapping("/{currentPage}/{pageSize}")
//    public R getPage(@PathVariable int currentPage, @PathVariable int pageSize, PostComment postComment){
//        IPage<PostComment> page = commentService.getPage(currentPage, pageSize, postComment);
//        //如果当前要查询的页码值大于总页码值，默认查询最后一页数据
//        if(currentPage > page.getPages()){
//            page = commentService.getPage((int) page.getPages(), pageSize, postComment);
//        }
//        return new R(true, page);
//    }
//
//    //添加评论
//    @PostMapping
//    public R addComment(@RequestBody @Valid PostComment postComment, BindingResult result){
//        R r = new R();
//        //jsr 303验证
//        if (result.hasErrors()) {
//            r.setStatus(false);
//            r.setMessage(result.getFieldError().getDefaultMessage());
//        }else {
//            Boolean flag = commentService.addComment(postComment);
//            r.setStatus(flag);
//            r.setMessage(flag ? "评论成功" : "评论失败，请稍后再试！");
//        }
//        return r;
//    }
//
//    //获取一个帖子的评论列表  根据帖子id查询
//    @GetMapping("/getAllByPid/{postId}")
//    public R getAllByPid(@PathVariable Integer postId){
//        return new R(true, commentService.getAllByPid(postId));
//    }
//
//
//    //删除评论
//    @DeleteMapping("/{id}")
//    public R removeById(@PathVariable Integer id){
//        boolean flag = commentService.deleteById(id);
//        return new R(flag, flag ? "删除成功" : "删除失败");
//    }
//
//    //置顶评论 取消置顶
//    @PatchMapping("/sticky/{id}")
//    public R sticky(@PathVariable Integer id){
//        Boolean flag = commentService.sticky(id);
//        return new R(flag, flag ? "操作成功" : "操作失败，请稍后再试！");
//    }
//
//    //查询用户收到的评论
//    @GetMapping("/getByUid/{userId}")
//    public R getByUid(@PathVariable Integer userId){
//        return new R(true, commentService.getByUid(userId));
//    }
//
//    //根据id查询
//    @GetMapping("/{id}")
//    public R getById(@PathVariable Integer id){
//        PostComment postComment = commentService.getById(id);
//        Boolean flag = postComment != null;
//        return new R(flag, postComment, flag?"查询成功":"查询失败，请稍后再试！");
//    }
//
//    //根据id修改
//    @PutMapping
//    public R updateById(@RequestBody @Valid PostComment postComment, BindingResult result){
//        R r = new R();
//        //jsr 303验证
//        if (result.hasErrors()) {
//            r.setStatus(false);
//            r.setMessage(result.getFieldError().getDefaultMessage());
//        }else {
//            Boolean flag = commentService.updateById(postComment);
//            r.setStatus(flag);
//            r.setMessage(flag ? "修改成功" : "操作失败，请稍后再试！");
//        }
//        return r;
//    }
//}
